package com.camunda.workflow.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.mybatisflex.core.mask.MaskManager;
import com.mybatisflex.core.paginate.Page;
import com.camunda.workflow.common.base.RestResp;
import com.camunda.workflow.config.saToken.LoginHelper;
import com.camunda.workflow.domain.entity.SysUser;
import com.camunda.workflow.domain.req.SysUserReq;
import com.camunda.workflow.domain.resp.SysUserResp;
import com.camunda.workflow.service.SysUserService;
import com.camunda.workflow.utils.EncryUtil;
import com.camunda.workflow.utils.ObjUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author pop
 */

@Slf4j
@Api(tags = "系统管理-用户")
@ApiSort(value = 1)
@RequestMapping("/test/system/user")
@RestController
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    @ApiOperationSupport(order = 1)
    @SaIgnore
    @ApiOperation("登陆")
    @PostMapping("/login")
    public RestResp<SysUserResp> login(@RequestBody SysUser sysUser) {
        String username = sysUser.getUsername();
        String password = sysUser.getPassword();
        if (ObjUtil.isNull(username)) {
            return RestResp.fail("用户名不能为空");
        }
        if (ObjUtil.isNull(password)) {
            return RestResp.fail("密码不能为空");
        }
        // 脱敏处理
        SysUserResp user = MaskManager.execWithoutMask(() -> sysUserService.getByUsername(username));
//        SysUserResp user = userService.getByUsername(username);
        if (Objects.isNull(user)) {
            return RestResp.fail("用户名错误");
        }
        if (!EncryUtil.passwdMatch(password, user.getPassword())) {
            return RestResp.fail("密码错误");
        }
        //查询 用户角色和菜单信息
        LoginHelper.login(user);
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        user.setToken(tokenInfo.tokenValue);
        user.setPassword("");
        // 第3步，返回给前端
        return RestResp.ok(user);
    }

    @ApiOperationSupport(order = 2)
    @SaIgnore
    @ApiOperation("退出")
    @GetMapping("/logout")
    public RestResp<?> logout() {
        StpUtil.logout();
        return RestResp.ok();
    }

    @ApiOperationSupport(order = 3)
    @ApiParam(value = "密码", name = "password", type = "String")
    @ApiOperation("重置密码")
    @PutMapping("/resetPasswd")
    public RestResp<?> resetPasswd(String password) {
        if (ObjUtil.isNull(password)) {
            return RestResp.fail("密码不能为空");
        }
        SysUserResp user = LoginHelper.getLoginUser();
        boolean i = sysUserService.resetPasswd(user.getId(), password);
        return i ? RestResp.ok() : RestResp.fail();
    }


    @ApiOperationSupport(order = 4)
    @ApiOperation("分页查询")
    @GetMapping("/getPage")
    public RestResp<Page<SysUserResp>> getPage(SysUserReq req) {
        Page<SysUserResp> page = sysUserService.getPage(req);
        return RestResp.ok(page);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("条件查询")
    @GetMapping("/getList")
    public RestResp<List<SysUserResp>> getList(SysUserReq req) {
        List<SysUserResp> page = sysUserService.getList(req);
        return RestResp.ok(page);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation("根据ID查询")
    @GetMapping("/getById")
    public RestResp<SysUserResp> getById(String userId) {
        SysUserResp resp = sysUserService.getById(userId);
        return RestResp.ok(resp);
    }

    @ApiOperationSupport(order = 7)
//    @ApiImplicitParam(value = "用户信息（ID不存在）", name = "SysUserReq", dataType = "SysUserReq", dataTypeClass = SysUserReq.class)
    @ApiOperation("保存")
    @PostMapping("/save")
    public RestResp<?> save(@RequestBody SysUserReq req) {
        boolean i = sysUserService.saveAndEdit(req);
        return i ? RestResp.ok() : RestResp.fail();
    }

    @ApiOperationSupport(order = 8)
//    @ApiImplicitParam(value = "用户信息（ID存在）", name = "SysUserReq", dataType = "SysUserReq", dataTypeClass = SysUserReq.class)
    @ApiOperation("编辑")
    @PutMapping("/edit")
    public RestResp<?> edit(@RequestBody SysUserReq req) {
        boolean i = sysUserService.saveAndEdit(req);
        return i ? RestResp.ok() : RestResp.fail();
    }

    @ApiOperationSupport(order = 9)
//    @ApiImplicitParam(value = "id集合", name = "ids", dataType = "List<Long>", dataTypeClass = List.class)
    @ApiOperation("删除")
    @PostMapping("/deleteByIds")
    public RestResp<?> deleteByIds(@RequestBody List<String> ids) {
        boolean i = sysUserService.deleteByIds(ids);
        return i ? RestResp.ok() : RestResp.fail("参数不能为空");
    }
}
